load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel:canisters.bzl", "rust_canister")
load("//bazel:defs.bzl", "rust_ic_test_suite")

package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-http-types",
    "//rs/interfaces/registry",
    "//rs/nervous_system/canisters",
    "//rs/nervous_system/common",
    "//rs/nervous_system/timer_task",
    "//rs/nns/constants",
    "//rs/node_rewards/canister/api",
    "//rs/node_rewards/rewards_calculation",
    "//rs/protobuf",
    "//rs/registry/canister-client",
    "//rs/registry/keys",
    "//rs/registry/node_provider_rewards",
    "//rs/types/base_types",
    "//rs/types/types",
    "@crate_index//:candid",
    "@crate_index//:chrono_canisters",
    "@crate_index//:futures",
    "@crate_index//:ic-cdk",
    "@crate_index//:ic-cdk-timers",
    "@crate_index//:ic-management-canister-types",
    "@crate_index//:ic-metrics-encoder",
    "@crate_index//:ic-stable-structures",
    "@crate_index//:indexmap",
    "@crate_index//:itertools",
    "@crate_index//:prost",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//packages/pocket-ic",
    "//rs/nervous_system/agent:test_agent",
    "//rs/nervous_system/integration_tests:nervous_system_integration_tests",
    "//rs/nns/test_utils",
    "//rs/registry/transport",
    "//rs/state_machine_tests",
    "@crate_index//:candid_parser",
    "@crate_index//:futures-util",
    "@crate_index//:maplit",
    "@crate_index//:mockall",
    "@crate_index//:pretty_assertions",
    "@crate_index//:serde_json",
    "@crate_index//:tokio",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:async-trait",
]

DEV_MACRO_DEPENDENCIES = [
    # Keep sorted.
]

DEV_DATA = [
    # Keep sorted.
    ":node-rewards-canister",
    ":node-rewards-canister-test",
    "//rs/ledger_suite/icp/ledger:ledger-canister-wasm",
    "//rs/nns/cmc:cycles-minting-canister",
    "//rs/nns/governance:governance-canister",
    "//rs/nns/governance:governance-canister-test",
    "//rs/nns/gtc:genesis-token-canister",
    "//rs/nns/handlers/lifeline/impl:lifeline_canister",
    "//rs/nns/handlers/root/impl:root-canister",
    "//rs/nns/sns-wasm:sns-wasm-canister",
    "//rs/pocket_ic_server:pocket-ic-server",
    "//rs/registry/canister:registry-canister",
]

DEV_ENV = {
    # Keep sorted.
    "CARGO_MANIFEST_DIR": "rs/node_rewards/canister",
    "CYCLES_MINTING_CANISTER_WASM_PATH": "$(rootpath //rs/nns/cmc:cycles-minting-canister)",
    "NODE_REWARDS_CANISTER_WASM_PATH": "$(rootpath //rs/node_rewards/canister:node-rewards-canister)",
    "NODE_REWARDS_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/node_rewards/canister:node-rewards-canister-test)",
    "GENESIS_TOKEN_CANISTER_WASM_PATH": "$(rootpath //rs/nns/gtc:genesis-token-canister)",
    "GOVERNANCE_CANISTER_WASM_PATH": "$(rootpath //rs/nns/governance:governance-canister)",
    "GOVERNANCE_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/nns/governance:governance-canister-test)",
    "LEDGER_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icp/ledger:ledger-canister-wasm)",
    "LIFELINE_CANISTER_WASM_PATH": "$(rootpath //rs/nns/handlers/lifeline/impl:lifeline_canister)",
    "REGISTRY_CANISTER_WASM_PATH": "$(rootpath //rs/registry/canister:registry-canister)",
    "ROOT_CANISTER_WASM_PATH": "$(rootpath //rs/nns/handlers/root/impl:root-canister)",
    "SNS_WASM_CANISTER_WASM_PATH": "$(rootpath //rs/nns/sns-wasm:sns-wasm-canister)",
    "POCKET_IC_BIN": "$(rootpath //rs/pocket_ic_server:pocket-ic-server)",
}

LIB_SRCS = glob(
    [
        "src/**/*.rs",
    ],
    exclude = ["tests*/**"],
)

rust_library(
    name = "nrc",
    srcs = LIB_SRCS,
    crate_name = "ic_node_rewards_canister",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES,
)

rust_library(
    name = "nrc-test",
    srcs = LIB_SRCS,
    crate_features = ["test"],
    crate_name = "ic_node_rewards_canister",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES,
)

rust_canister(
    name = "node-rewards-canister",
    srcs = ["src/main.rs"],
    proc_macro_deps = MACRO_DEPENDENCIES,
    service_file = ":node-rewards-canister.did",
    deps = DEPENDENCIES + [
        ":nrc",
    ],
)

rust_canister(
    name = "node-rewards-canister-test",
    srcs = ["src/main.rs"],
    crate_features = ["test"],
    proc_macro_deps = MACRO_DEPENDENCIES,
    service_file = ":node-rewards-canister.did",
    deps = DEPENDENCIES + [
        ":nrc-test",
    ],
)

rust_test(
    name = "main_test",
    srcs = ["src/main.rs"],
    compile_data = ["node-rewards-canister.did"],
    crate_root = "src/main.rs",
    proc_macro_deps = MACRO_DEPENDENCIES + DEV_MACRO_DEPENDENCIES,
    deps = DEV_DEPENDENCIES + DEPENDENCIES + [
        ":nrc",
    ],
)

rust_test(
    name = "nrc_test",
    crate = ":nrc",
    proc_macro_deps = MACRO_DEPENDENCIES + DEV_MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_ic_test_suite(
    name = "node_rewards_canister_integration_test",
    srcs = glob(
        ["tests/*.rs"],
        exclude = [
            # Keep sorted.
            "tests/mod.rs",
        ],
    ),
    data = DEV_DATA,
    env = DEV_ENV,
    proc_macro_deps = MACRO_DEPENDENCIES + DEV_MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":nrc"],
)
